package com.example.controller;

import com.example.common.Result;
import com.example.model.entity.Grade;
import com.example.model.dto.GradeRequest;
import com.example.model.entity.UserInfo;
import com.example.model.vo.GradeVo;
import com.example.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/backend/grade")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    // 教师查看所有成绩
    @GetMapping("/list")
    public Result<List<GradeVo>> listAllGrades() {
        return Result.success(gradeService.listAllGrades());
    }

    // 学生查看自己的成绩
    @GetMapping("/student/list")
    public Result<List<Grade>> listStudentGrades(Integer page,Integer limit ,HttpSession session) {
        UserInfo user = (UserInfo) session.getAttribute("user");
        return Result.success(gradeService.listStudentGrades(user.getId()));
    }

    // 教师添加成绩
    @PostMapping("/add")
    public Result<Grade> addGrade( @RequestBody GradeRequest gradeRequest) {
        return Result.success(gradeService.addGrade(gradeRequest));
    }

    // 教师修改成绩
    @PostMapping("/edit")
    public Result<Grade> updateGrade(@RequestBody GradeRequest gradeRequest) {
        return Result.success(gradeService.updateGrade(gradeRequest));
    }

    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody Grade grade) {
        return Result.success(gradeService.delete(grade));
    }
} 